﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using System.Text;
//using log4net;
using System.Reflection;

public partial class ShopManagerScoreCount : ExtBaseRpt
{
    mrjapp mrj = new mrjapp();
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            try
            {
                InsActLog(mid, "浏览");
                string strSql = string.Format(@"SELECT DISTINCT OperatorAreaName FROM t_task_shop_permissions WHERE Display='{0}'", Session["UserName"].ToString());
                DataSet ds = mydb.DM_GetDateSet(strSql);
                if (ds != null && ds.Tables[0].Rows.Count > 0) //控制陈列师查看的数据权限
                {
                    this.txtOperatorAreaName.Text = ds.Tables[0].Rows[0]["OperatorAreaName"].ToString();
                    GetBigAreaName(null, null);
                    txtOperatorAreaName.GetStore().DataSource = mydb.DM_GetDateSet(strSql);
                    string sqlDisplay = string.Format(@"SELECT DISTINCT Display FROM t_task_shop_permissions WHERE ShopState<>'已撤店' AND Display IS NOT NULL AND OperatorAreaName IN ({0}) ", strSql);
                    txtDisplay.GetStore().DataSource = mydb.DM_GetDateSet(sqlDisplay);
                    string sqlShopManager = string.Format(@"SELECT DISTINCT ShopManager FROM t_task_shop_permissions WHERE ShopState<>'已撤店' AND ShopManager IS NOT NULL AND OperatorAreaName IN ({0}) ", strSql);
                    txtShopManager.GetStore().DataSource = mydb.DM_GetDateSet(sqlShopManager);
                }
                else
                {
                    txtOperatorAreaName.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT OperatorAreaName FROM t_task_shop_permissions ");
                    txtDisplay.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT Display FROM t_task_shop_permissions WHERE ShopState<>'已撤店' AND Display IS NOT NULL ");
                    txtShopManager.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT ShopManager FROM t_task_shop_permissions WHERE ShopState<>'已撤店' AND ShopManager IS NOT NULL ");
                }
            }
            catch (Exception ex)
            {
                ShowBox("错误：" + ex.Message);
            }
        }
        if (Session["UserID"] == null)
        {
            ShowBox("用户登入超时请求重新登入");
        }
    }

    //获取省份
    protected void GetBigAreaName(object sender, DirectEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txtOperatorAreaName.Text))
        {
            txtBigAreaName.Text = string.Empty;
            txtBigAreaName.GetStore().RemoveAll();
            txtAreaName.Text = string.Empty;
            txtAreaName.GetStore().RemoveAll();
        }
        else
        {
            //先清空数据后再绑定
            txtBigAreaName.Text = string.Empty;
            txtBigAreaName.GetStore().RemoveAll();
            txtAreaName.Text = string.Empty;
            txtAreaName.GetStore().RemoveAll();

            string sql = string.Format(@"SELECT DISTINCT ProvinceName AS BigAreaName FROM t_task_shop_permissions WHERE OperatorAreaName='{0}' ", txtOperatorAreaName.Text);
            txtBigAreaName.GetStore().DataSource = mydb.DM_GetDateSet(sql);
            this.txtBigAreaName.DataBind();
        }
    }

    //获取城市
    protected void GetArea(object sender, DirectEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txtBigAreaName.Text))
        {
            txtAreaName.Text = string.Empty;
            txtAreaName.GetStore().RemoveAll();
        }
        else
        {
            //先清空数据后再绑定
            txtAreaName.Text = string.Empty;
            txtAreaName.GetStore().RemoveAll();

            txtAreaName.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT AreaName FROM t_task_shop_permissions WHERE ProvinceName='" + txtBigAreaName.Value + "' ");
            this.Store_AreaName.DataBind();
        }
    }

    protected void StartDateField(object sender, DirectEventArgs e)
    {
        this.txtProcessingTimeEnd.MinDate = Convert.ToDateTime(this.txtProcessingTimeBegin.Text);
        int Begin = Convert.ToInt32(Convert.ToDateTime(this.txtProcessingTimeBegin.Text).DayOfWeek);
        if (Begin == 0)
        {
            this.txtProcessingTimeEnd.MaxDate = Convert.ToDateTime(this.txtProcessingTimeBegin.Text);
            this.txtProcessingTimeEnd.Text = this.txtProcessingTimeBegin.Text;
        }
        else
        {
            this.txtProcessingTimeEnd.MaxDate = Convert.ToDateTime(this.txtProcessingTimeBegin.Text).AddDays(7 - Begin);
            if (Convert.ToDateTime(this.txtProcessingTimeEnd.Text) < Convert.ToDateTime(this.txtProcessingTimeBegin.Text))
            {
                this.txtProcessingTimeEnd.Text = this.txtProcessingTimeBegin.Text;
            }
            else if (Convert.ToDateTime(this.txtProcessingTimeEnd.Text) > Convert.ToDateTime(this.txtProcessingTimeBegin.Text).AddDays(7 - Begin))
            {
                this.txtProcessingTimeEnd.Text = Convert.ToDateTime(this.txtProcessingTimeBegin.Text).AddDays(7 - Begin).ToString();
            }
        }

    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null, null);
    }

    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        try
        {
            string strProcessingTimeBegin = this.txtProcessingTimeBegin.Text;
            string strProcessingTimeEnd = this.txtProcessingTimeEnd.Text;
            if (string.IsNullOrEmpty(strProcessingTimeBegin) || Convert.ToDateTime(strProcessingTimeBegin) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入开始日期条件！");
                return;
            }
            if (string.IsNullOrEmpty(strProcessingTimeEnd) || Convert.ToDateTime(strProcessingTimeEnd) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入结束日期条件！");
                return;
            }
            
            this.Store1.DataSource = GetShopManagerScoreCount(strProcessingTimeBegin, strProcessingTimeEnd);
            this.Store1.DataBind();
        }
        catch (Exception ex)
        {
            ShowBox("查询出错：" + ex.Message);
        }
    }

    protected void BtnScoreCountOutExcel(object sender, DirectEventArgs e)
    {
        try
        {
            string strProcessingTimeBegin = this.txtProcessingTimeBegin.Text;
            string strProcessingTimeEnd = this.txtProcessingTimeEnd.Text;
            if (string.IsNullOrEmpty(strProcessingTimeBegin) || Convert.ToDateTime(strProcessingTimeBegin) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入开始日期条件！");
                return;
            }
            if (string.IsNullOrEmpty(strProcessingTimeEnd) || Convert.ToDateTime(strProcessingTimeEnd) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入结束日期条件！");
                return;
            }
            
            DataSet ds = GetShopManagerScoreCount(strProcessingTimeBegin, strProcessingTimeEnd);
            string path = NpoiHelper.ExportByWeb(mid, ds, this.Title);
            this.ResourceManager1.AddScript("outExcle('" + path + "')");

        }
        catch (Exception ex)
        {
            Activity.Log.Error("请求统计数据异常：" + ex);
            ShowBox("导出出错：" + ex.Message);
        }
    }

    public DataSet GetShopManagerScoreCount(string strProcessingTimeBegin, string strProcessingTimeEnd)
    {
        try
        {
            strProcessingTimeBegin = Convert.ToDateTime(strProcessingTimeBegin).ToString("yyyy-MM-dd");
            strProcessingTimeEnd = Convert.ToDateTime(strProcessingTimeEnd).ToString("yyyy-MM-dd");
            string strCheckCycle = strProcessingTimeBegin + "~" + strProcessingTimeEnd;

            string strOperatorAreaName = this.txtOperatorAreaName.Text;
            string strBigAreaName = this.txtBigAreaName.Text;
            string strAreaName = this.txtAreaName.Text;
            string strDisplay = this.txtDisplay.Text;
            string strShopManager = this.txtShopManager.Text;

            string sql = string.Format(@" exec ShopManagerScoreCount  '{0}','{1}','{2} 23:59:59','{3}','{4}','{5}','{6}','{7}' ", strCheckCycle, strProcessingTimeBegin, strProcessingTimeEnd, strOperatorAreaName, strBigAreaName, strAreaName, strDisplay, strShopManager);

            return mydb.DM_GetDateSet(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}